package com.efounder.form.view;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.efounder.form.model.CompanyModel;
import com.efounder.mobilecomps.R;

import java.util.ArrayList;


public class CheckBoxPopWindow extends PopupWindow {
    private View conentView;
    private ListView listView;
    private Button cancelButton;
    private Button searchButton;
    private Button enterButton;
    private EditText searchEditText;
    private ArrayList<CompanyModel> listData = new ArrayList<>();
    private ArrayList<CompanyModel> templistData = new ArrayList<>();
    private ArrayList<CompanyModel> selectListData = new ArrayList<>();


    private CrazyAdapter mAdapter;
    private SelectModelCallBack callBack;

    public CheckBoxPopWindow(final Activity context, final ArrayList<CompanyModel> data) {

        this.listData.addAll(data);
        this.templistData.addAll(data);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.checkbox_popwindow, null);

        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽

        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        int height = wm.getDefaultDisplay().getHeight();
        this.setHeight(height / 2);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        backgroundAlpha(context,0.5f);
//        // 设置SelectPicPopupWindow弹出窗体动画效果
        //设置取消 半透明
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(context, 1f);
            }
        });



        listView = (ListView) conentView.findViewById(R.id.pop_listview);
        cancelButton = (Button) conentView.findViewById(R.id.pop_cannel);
        searchButton = (Button) conentView.findViewById(R.id.pop_search);
        searchEditText = (EditText) conentView.findViewById(R.id.pop_edit);

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = searchEditText.getText().toString();
                if (s == null || "".equals(s.toString())) {
                    searchButton.setVisibility(View.GONE);
                } else {
                    searchButton.setVisibility(View.VISIBLE);
                    listData.clear();
                    if (!"".equals(s)) {

                        for (int i = 0; i < templistData.size(); i++) {
                            if (templistData.get(i).getF_MC().contains(s)) {
                                listData.add(templistData.get(i));
                            }
                        }

                    } else {//还原原来数据
                        listData.addAll(templistData);
                    }

                    mAdapter.notifyDataSetChanged();
                }
            }
        });
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("confirm", "onTextChanged: " + count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("confirm", "afterTextChanged: " + s.toString());
                if (s == null || "".equals(s.toString())) {
                    searchButton.setVisibility(View.GONE);
                    listData.clear();
                    listData.addAll(templistData);
                    mAdapter.notifyDataSetChanged();
                } else {
                    searchButton.setVisibility(View.VISIBLE);
                }
            }
        });
        enterButton = (Button) conentView.findViewById(R.id.pop_enter);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {

                    callBack.getSelectedModel(selectListData);
                }
                dismiss();
            }
        });

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setItemsCanFocus(false);

        mAdapter = new CrazyAdapter(context, listData);
        listView.setAdapter(mAdapter);
    }

    public class CrazyAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<CompanyModel> mList;

        public CrazyAdapter(Context mContext, ArrayList<CompanyModel> mList) {
            this.mContext = mContext;
            this.mList = mList;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.checkbox_popwindow_item, null);
                holder = new ViewHolder();
                holder.view = (TextView) convertView.findViewById(R.id.tv_title);
                holder.box = (CheckBox) convertView.findViewById(R.id.cb_box);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        buttonView.setChecked(true);
                        if (!selectListData.contains(listData.get(position))) {
                            selectListData.add(listData.get(position));
                        }
                    } else {

                        buttonView.setChecked(false);
                        if (selectListData.contains(listData.get(position))) {
                            selectListData.remove(listData.get(position));
                        }
                    }
                }
            });
            holder.view.setText(mList.get(position).getF_MC());
            holder.box.setChecked(isSelected(listData.get(position)));

            return convertView;
        }

        public boolean isSelected(CompanyModel model) {
            return selectListData.contains(model);
        }

        public final class ViewHolder {
            private TextView view;
            private CheckBox box;
        }


    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha)
    {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
    public interface SelectModelCallBack {
        public void getSelectedModel(ArrayList<CompanyModel> list);
    }

    public void setSelectModelCallBack(SelectModelCallBack callBack) {
        this.callBack = callBack;
    }
}